package chapter1;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Response {
	private Request request;
	private OutputStream os;
	public Request getRequest() {
		return request;
	}
	public void setRequest(Request request) {
		this.request = request;
	}
	public OutputStream getOs() {
		return os;
	}
	public void setOs(OutputStream os) {
		this.os = os;
	}
	public Response(OutputStream os){
		this.os=os;
	}
	public void sendResource() throws IOException{
		byte[] buffer= new byte[1024];
		File file=null;
		FileInputStream fis=null;
		try {
			file =new File(HttpServer.WWW_ROOT,request.getUri());
			if(file.exists()){
				fis=new FileInputStream(file);
				InputStream is= new BufferedInputStream(fis);
				while((is.read(buffer))!=-1){
					os.write(buffer);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			if(fis!=null){
				fis.close();
			}
		}
	}
}
